package com.cskaoyan.market.admin.service.impl;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.session.SqlSession;

import com.cskaoyan.market.admin.service.MarketAdminUserService;
import com.cskaoyan.market.db.domain.*;
import com.cskaoyan.market.db.mapper.*;
import com.cskaoyan.market.util.MyBatisUtil;
import com.github.pagehelper.PageHelper;

/**
 * @author BlackFlame33
 * @date 2023/03/19
 **/
public class MarketAdminUserServiceImpl implements MarketAdminUserService {
    @Override
    public List<MarketUser> getUserList(Integer page, Integer limit, String sort, String order, String username,
        String mobile) {
        SqlSession session = MyBatisUtil.getSession();
        MarketUserMapper mapper = session.getMapper(MarketUserMapper.class);
        MarketUserExample marketUserExample = new MarketUserExample();
        MarketUserExample.Criteria criteria = marketUserExample.createCriteria();
        if (StringUtils.isNotEmpty(username)) {
            criteria.andUsernameLike("%" + username + "%");
        }
        if (StringUtils.isNotEmpty(mobile)) {
            criteria.andMobileLike("%" + mobile + "%");
        }
        marketUserExample.setOrderByClause(sort + " " + order);
        PageHelper.startPage(page, limit);
        List<MarketUser> marketUsers = mapper.selectByExample(marketUserExample);
        session.commit();
        session.close();
        return marketUsers;
    }

    @Override
    public List<MarketAddress> getAddressList(Integer page, Integer limit, String sort, String order, String name,
        Integer userId) {
        SqlSession session = MyBatisUtil.getSession();
        MarketAddressMapper mapper = session.getMapper(MarketAddressMapper.class);
        MarketAddressExample marketAddressExample = new MarketAddressExample();
        MarketAddressExample.Criteria criteria = marketAddressExample.createCriteria();
        if (StringUtils.isNotEmpty(name)) {
            criteria.andNameLike("%" + name + "%");
        }
        if (userId != -1) {
            criteria.andUserIdEqualTo(userId);
        }
        marketAddressExample.setOrderByClause(sort + " " + order);
        PageHelper.startPage(page, limit);
        List<MarketAddress> marketAddresses = mapper.selectByExample(marketAddressExample);
        session.commit();
        session.close();
        return marketAddresses;
    }

    @Override
    public List<MarketCollect> getCollectList(Integer page, Integer limit, String sort, String order, int userId,
        int valuedId) {
        SqlSession session = MyBatisUtil.getSession();
        MarketCollectMapper mapper = session.getMapper(MarketCollectMapper.class);
        MarketCollectExample marketCollectExample = new MarketCollectExample();
        MarketCollectExample.Criteria criteria = marketCollectExample.createCriteria();
        if (userId != -1) {
            criteria.andUserIdEqualTo(userId);
        }
        if (valuedId != -1) {
            criteria.andValueIdEqualTo(valuedId);
        }
        marketCollectExample.setOrderByClause(sort + " " + order);
        PageHelper.startPage(page, limit);
        List<MarketCollect> marketCollects = mapper.selectByExample(marketCollectExample);
        session.commit();
        session.close();
        return marketCollects;
    }

    @Override
    public List<MarketFootprint> getFootprintList(Integer page, Integer limit, String sort, String order, int userId,
        int goodsId) {
        SqlSession session = MyBatisUtil.getSession();
        MarketFootprintMapper mapper = session.getMapper(MarketFootprintMapper.class);
        MarketFootprintExample marketFootprintExample = new MarketFootprintExample();
        MarketFootprintExample.Criteria criteria = marketFootprintExample.createCriteria();
        if (userId != -1) {
            criteria.andUserIdEqualTo(userId);
        }
        if (goodsId != -1) {
            criteria.andGoodsIdEqualTo(goodsId);
        }
        marketFootprintExample.setOrderByClause(sort + " " + order);
        PageHelper.startPage(page, limit);
        List<MarketFootprint> marketFootprints = mapper.selectByExample(marketFootprintExample);
        session.commit();
        session.close();
        return marketFootprints;
    }

    @Override
    public List<MarketSearchHistory> getHistoryList(Integer page, Integer limit, String sort, String order, int userId,
        String keyword) {
        SqlSession session = MyBatisUtil.getSession();
        MarketSearchHistoryMapper mapper = session.getMapper(MarketSearchHistoryMapper.class);
        MarketSearchHistoryExample marketSearchHistoryExample = new MarketSearchHistoryExample();
        MarketSearchHistoryExample.Criteria criteria = marketSearchHistoryExample.createCriteria();
        if (userId != -1) {
            criteria.andUserIdEqualTo(userId);
        }
        if (StringUtils.isNotEmpty(keyword)) {
            criteria.andKeywordLike("%" + keyword + "%");
        }
        marketSearchHistoryExample.setOrderByClause(sort + " " + order);
        PageHelper.startPage(page, limit);
        List<MarketSearchHistory> marketSearchHistories = mapper.selectByExample(marketSearchHistoryExample);
        session.commit();
        session.close();
        return marketSearchHistories;
    }

    @Override
    public List<MarketFeedback> getFeedbackList(Integer page, Integer limit, String sort, String order, String username,
        int id) {
        SqlSession session = MyBatisUtil.getSession();
        MarketFeedbackMapper mapper = session.getMapper(MarketFeedbackMapper.class);
        MarketFeedbackExample marketFeedbackExample = new MarketFeedbackExample();
        MarketFeedbackExample.Criteria criteria = marketFeedbackExample.createCriteria();
        if (StringUtils.isNotEmpty(username)) {
            criteria.andUsernameLike("%" + username + "%");
        }
        if (id != -1) {
            criteria.andIdEqualTo(id);
        }
        marketFeedbackExample.setOrderByClause(sort + " " + order);
        PageHelper.startPage(page, limit);
        List<MarketFeedback> marketFeedbacks = mapper.selectByExample(marketFeedbackExample);
        session.commit();
        session.close();
        return marketFeedbacks;
    }
}